<template>
  <div class="admin-layout">
    <el-container class="layout-container">
      <!-- 侧边栏 -->
      <el-aside width="230px" class="sidebar">
        <div class="logo">
          <h2>高校成果管理系统</h2>
        </div>
        <el-menu
          :default-active="activeMenu"
          class="sidebar-menu"
          :router="true"
          background-color="#f5f5f5"
          text-color="#555"
          active-text-color="#333"
        >
          <el-menu-item index="/admin/dashboard">
            <el-icon><el-icon-monitor /></el-icon>
            <span>首页</span>
          </el-menu-item>
          <el-menu-item index="/admin/users">
            <el-icon><el-icon-user /></el-icon>
            <span>教师用户管理</span>
          </el-menu-item>
          <el-menu-item index="/admin/profile">
            <el-icon><el-icon-user-filled /></el-icon>
            <span>个人信息</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <el-container>
        <!-- 顶部栏 -->
        <el-header class="header">
          <div class="header-left">
            <el-icon class="fold-icon" @click="toggleSidebar">
              <el-icon-fold v-if="!collapsed" />
              <el-icon-expand v-else />
            </el-icon>
          </div>
          <div class="header-right">
            <el-dropdown trigger="click" @command="handleCommand">
              <span class="user-dropdown-link">
                {{ userName }}
                <el-icon class="el-icon--right"><el-icon-arrow-down /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                  <el-dropdown-item command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>

        <!-- 主内容区域 -->
        <el-main class="main-content">
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script lang="ts">
import { computed, defineComponent, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '../../store/auth'
import { ElMessage } from 'element-plus'
import {
  Monitor as ElIconMonitor,
  User as ElIconUser,
  UserFilled as ElIconUserFilled,
  Fold as ElIconFold,
  Expand as ElIconExpand,
  ArrowDown as ElIconArrowDown
} from '@element-plus/icons-vue'

export default defineComponent({
  name: 'AdminLayout',
  components: {
    ElIconMonitor,
    ElIconUser,
    ElIconUserFilled,
    ElIconFold,
    ElIconExpand,
    ElIconArrowDown
  },
  setup() {
    const router = useRouter()
    const authStore = useAuthStore()
    
    const collapsed = ref(false)
    const activeMenu = computed(() => router.currentRoute.value.path)
    const userName = computed(() => {
      const user = authStore.user
      return user ? user.username : '管理员'
    })
    
    // 切换侧边栏
    const toggleSidebar = () => {
      collapsed.value = !collapsed.value
    }
    
    // 下拉菜单命令处理
    const handleCommand = (command: string) => {
      if (command === 'logout') {
        authStore.logout()
        router.push('/login')
        ElMessage.success('已退出登录')
      } else if (command === 'profile') {
        router.push('/admin/profile')
      }
    }
    
    return {
      collapsed,
      activeMenu,
      userName,
      toggleSidebar,
      handleCommand
    }
  }
})
</script>

<style lang="scss" scoped>
.admin-layout {
  height: 100vh;
  
  .layout-container {
    height: 100%;
  }
  
  .sidebar {
    background-color: #f5f5f5;
    height: 100%;
    overflow-x: hidden;
    border-right: 1px solid rgba(0,0,0,0.06);
    
    .logo {
      height: 60px;
      display: flex;
      align-items: center;
      justify-content: center;
      border-bottom: 1px solid rgba(0,0,0,0.06);
      
      h2 {
        color: #333;
        font-size: 18px;
        margin: 0;
        padding: 0 10px;
        white-space: nowrap;
        font-weight: 500;
      }
    }
    
    .sidebar-menu {
      border-right: none;
      
      &.el-menu {
        background-color: #f5f5f5;
      }
      
      .el-menu-item {
        color: #555;
        height: 56px;
        line-height: 56px;
        
        &.is-active {
          color: #333;
          background-color: #e8e8e8;
        }
        
        &:hover {
          background-color: #e8e8e8;
        }
      }
    }
  }
  
  .header {
    background-color: #fff;
    border-bottom: 1px solid rgba(0,0,0,0.06);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    box-shadow: 0 1px 4px rgba(0,0,0,0.03);
    
    .header-left {
      .fold-icon {
        font-size: 20px;
        cursor: pointer;
        color: #555;
      }
    }
    
    .header-right {
      .user-dropdown-link {
        cursor: pointer;
        display: flex;
        align-items: center;
        color: #333;
        font-weight: 500;
        
        .el-icon {
          margin-left: 5px;
          color: #555;
        }
      }
    }
  }
  
  .main-content {
    background-color: #f5f5f5;
    padding: 20px;
  }
}
</style> 